package com.taorenqi.business.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SP工具类
 * Created by Administrator on 2016/6/15.
 */
public class SpUtils {

    private static final String FILE_NAME = "config";

    public static void putBoolean(Context context, String key, boolean flag) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean(key, flag);
        edit.commit();
    }

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        boolean b = sp.getBoolean(key, defaultValue);
        return b;
    }

    public static void putString(Context context, String key, String flag) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(key, flag);
        edit.commit();
    }

    public static String getString(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        return sp.getString(key, "");
    }

    public static void putInt(Context context, String key, int flag) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt(key, flag);
        edit.commit();
    }

    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    public static void clearData(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

    public static void saveToken(Context context, String token){
        if(token != null){
            putString(context,"TOKEN",token);
        }
    }

    public static void saveMsgToken(Context context, String token){
        if(token != null){
            putString(context,"TOKEN_MSG",token);
        }
    }

    public static String getToken(Context context){
        String infoValue = getString(context, "TOKEN");
        return infoValue;

    }
    public static String getMsgToken(Context context){
        String infoValue = getString(context, "TOKEN_MSG");
        return infoValue;

    }


}
